/**
 * @author n0tnull
 *
 */
package fr.gadcparis.envieparisienne;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import fr.gadcparis.envieparisienne.model.Hit;
import fr.gadcparis.envieparisienne.model.OpenDataClient;
import greendroid.app.GDListActivity;
import greendroid.widget.ActionBar;
import greendroid.widget.ItemAdapter;
import greendroid.widget.item.Item;
import greendroid.widget.item.TextItem;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Pair;
import android.view.View;
import android.widget.ListView;

public class SearchRestaurantActivity extends GDListActivity{
	private Context context;
	private ArrayList<Hit> list_rest;
	private HashMap<String,String> pair;
	private List<Item> items = new ArrayList<Item>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActionBar aBar = getActionBar();
		aBar.setTitle(this.getString(R.string.search_restaurant));
		context = this;
		list_rest = new ArrayList<Hit>();
		pair = new HashMap<String,String>();

		getRestaurant gr = new getRestaurant();
		gr.execute();
	}

	private class getRestaurant extends AsyncTask{

		ProgressDialog pg;
		OpenDataClient client;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pg = new ProgressDialog(context);
			pg.setMessage(context.getString(R.string.wait_msg));
			pg.show();
		}

		@Override
		protected Object doInBackground(Object... params) {
			client = new OpenDataClient()
			.limit(10)
			.setTarget(OpenDataClient.HUNGER_CLIENT);

			//			client.near(loc, 1000);
			ArrayList<String> unik = new ArrayList<String>();
			items.add(new TextItem("Tous"));
			for(int i =0; i<client.count(); i++){
				Hit hit = client.getHit(i);
				list_rest.add(hit);

				if (hit.getCuisine() != null){
					if(!unik.contains(hit.getCuisine()) && hit.getCuisine().length() != 0){
						items.add(new TextItem(hit.getCuisine()));
						unik.add(hit.getCuisine());
						pair.put(hit.getCuisine(),hit.getMeta("cuisine", ""));
					}
				}
			}
			return null;
		}

		@Override
		protected void onPostExecute(Object result) {
			super.onPostExecute(result);
			final ItemAdapter adapter = new ItemAdapter(context,items);
			setListAdapter(adapter);
			pg.dismiss();
		}

	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);

		TextItem ti = (TextItem) getListView().getItemAtPosition(position);

		if (ti.text != "Tous"){

			String cuisine_choisie = pair.get(ti.text);

			ArrayList<Hit> result = new ArrayList<Hit>();

			for(Hit h: list_rest){
				if(h.getMeta("cuisine", "").equals(cuisine_choisie)){
					result.add(h);
				}
			}
			Singleton.getInstance().setMyList(result);
		} else{
			Singleton.getInstance().setMyList(list_rest);
		}
		
		Bundle b = new Bundle();
//		b.putParcelable(context.getString(R.string.listrestaurant), new RestaurantList(result));
		b.putInt(context.getString(R.string.category_needed), OpenDataClient.HUNGER_CLIENT );
		Intent i = new Intent(this, AroundMeActivity.class);
		i.putExtras(b);
		
		startActivity(i);
		
	}

//	
//	public class RestaurantList extends ArrayList<Hit> implements Parcelable{
//
//		public RestaurantList() {
//			// TODO Auto-generated constructor stub
//		}
//		
//		public RestaurantList(ArrayList<Hit> a){
//			super(a);
//		}
//		
//		public RestaurantList(Parcel in){
//			this();
//		}
//		
//		@SuppressWarnings("unchecked")
//		 
//        public static Parcelable.Creator CREATOR = new Parcelable.Creator() {
// 
//                public RestaurantList createFromParcel(Parcel in) {
// 
//                        return new RestaurantList(in);
// 
//                }
// 
// 
// 
//                public Object[] newArray(int arg0) {
// 
//                        return null;
// 
//                }
// 
// 
// 
//        };
// 
//       
// 
////        private void readFromParcel(Parcel in) {
//// 
////                this.clear();
//// 
//// 
//// 
////                //First we have to read the list size
//// 
////                int size = in.readInt();
//// 
//// 
//// 
////                //Reading remember that we wrote first the Name and later the Phone Number.
//// 
////                //Order is fundamental
//// 
////               
//// 
////                for (int i = 0; i < size; i++) {
//// 
////                        Hit c = new Hit();
//// 
////                        c.setName(in.readString());
//// 
////                        c.setPhoneNumber(in.readString());
//// 
////                        this.add(c);
//// 
////                }
//// 
////               
//// 
////        }
// 
// 
// 
//        public int describeContents() {
// 
//                return 0;
// 
//        }
//
//		public void writeToParcel(Parcel dest, int flags) {
//			// TODO Auto-generated method stub
//			int size = this.size();
//			dest.writeInt(size);
//		}
// 
// 
// 
////        public void writeToParcel(Parcel dest, int flags) {
//// 
////                int size = this.size();
//// 
////                //We have to write the list size, we need him recreating the list
//// 
////                dest.writeInt(size);
//// 
////                //We decided arbitrarily to write first the Name and later the Phone Number.
//// 
////                for (int i = 0; i < size; i++) {
//// 
////                        Hit c = this.get(i);
//// 
////                        dest.writeString(c.getName());
//// 
////                        dest.writeString(c.getPhoneNumber());
//// 
////                }
//// 
////        }
////		
//	}
	
}
